import io from '@hyoga/uni-socket.io';

class InitSocket {
	socket;
	constructor() {
		// 初始化socket
		this.socket = io('http://192.168.3.4:3000', {
			transports: ['websocket'],
			extraHeaders: {
				'Access-Control-Allow-Origin': '*',
			},
		})
		// 成功连接
		this.socket.on('connect', ()=>{ this.connect() })
		// 断开连接
		this.socket.on('disconnect', this.disconnect)
		// 连接重新建立时执行的操作
		this.socket.on('reconnect', this.reconnect)
		// 连接重新连接失败时执行的操作
		this.socket.on('reconnect_error', this.reconnectError)
		// 连接重新连接成功时执行的操作
		this.socket.on('reconnect_failed', this.reconnectFailed);
		// socket错误
		this.socket.on('error', this.error)
	}
	connect() {
		console.log('连接的Socket: ', this.socket)
		return this.socket
	}
	disconnect() {
		console.log('断开连接！')
	}
	reconnect(attempt) {
		console.log('重新连接，尝试次数：', attempt);
	}
	reconnectError() {
		console.log('重新连接失败！')
	}
	reconnectFailed() {
		console.log('重新连接成功！');
	}
	error(msg) {
		console.log('ws error', msg)
	}
}

export const initSocket = new InitSocket()
